package com.qf.controller;

import com.qf.entity.Emp;
import com.qf.entity.EmpManager;
import com.qf.service.EmpManagerService;
import com.qf.service.EmpService;
import com.qf.service.impl.EmpManagerServiceImpl;
import com.qf.service.impl.EmpServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/*
杨威
2021/9/16
19:41
*/
@WebServlet("/login")
public class LoginController extends HttpServlet {
    EmpManagerService empManagerService=new EmpManagerServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String pwd = request.getParameter("pwd");
        String check = request.getParameter("check");
        String code = (String)request.getSession().getAttribute("code");
        System.out.println("code = " + code);
        System.out.println("check="+check);
        if(code==null||!code.equalsIgnoreCase(check)){
            response.sendRedirect(request.getContextPath()+"/login.jsp");
            return;
        }
        EmpManager empManager = empManagerService.selectManagerByName(name, pwd);

        request.getSession().setAttribute("user",empManager);
        if(empManager!=null){
           response.sendRedirect(request.getContextPath()+"/ShowAll");
        }
        if(empManager==null){
            response.sendRedirect(request.getContextPath()+"/login.jsp");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
